/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Service;

import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.hibernate.Criteria;
import DB.HibernateUtil;
import DB.Usuario;

/**
 *
 * @author franzel
 */
@WebService()
public class UsuarioWS {

    @WebMethod(operationName = "agregarUsuario")
    public boolean agregarUsuario(@WebParam(name = "user") Usuario user) {

        return ControladorDeObjetos.saveObject(user);
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "listarUsuarios")
    public List<Usuario> listarUsuarios() {
        //TODO write your implementation code here:
        return ControladorDeObjetos.getAllObjectByClassType(Usuario.class);
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "cargarUsuario")
    public Object cargarUsuario(@WebParam(name = "rut") String rut) {
        //TODO write your implementation code here:
        return ControladorDeObjetos.loadObjectByStringId(rut, Usuario.class);
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "modificarUsuario")
    public boolean modificarUsuario(@WebParam(name = "Usuario") Usuario user) {
        return ControladorDeObjetos.updateObject(user);
    }

    @WebMethod(operationName = "filtrarUsuario")
    public List<Usuario> filtrarUsuario(@WebParam(name = "buscar") String buscar) {
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction trans = s.beginTransaction();
        Criteria c = s.createCriteria(Usuario.class).add(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.like("tipoCuenta", "%" + buscar + "%"),
                Restrictions.like("rut", "%" + buscar + "%")),
                Restrictions.or(Restrictions.like("usuario", "%" + buscar + "%"),
                Restrictions.like("email", "%" + buscar + "%"))),
                (Restrictions.like("celular", "%" + buscar + "%"))));
        List l = c.list();
        s.close();
        return l;
    }
}
